<?php

namespace App\Admin\Controllers;

use App\Models\AdminUser;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\UserController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class AdminUserController extends UserController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '后台用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    public function grid()
    {
        $grid = new Grid(new AdminUser);

        $grid->column('id', __('ID'))->sortable();
//        $grid->username('用户名')->sortable();
        $grid->column('username', trans('admin.username'));
        $grid->column('roles', trans('admin.roles'))->pluck('name')->label();
        $grid->column('created_at', trans('admin.created_at'));
        $grid->column('updated_at', __('admin.updated_at'));

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            if ($actions->getKey() == 1) {
                $actions->disableDelete();
            }
        });

        $grid->tools(function (Grid\Tools $tools) {
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
                $actions->disableDelete();
            });
        });
        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();
            $filter->like('username', '用户名')->inputmask();
            $filter->like('name', '名称')->inputmask();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    public function detail($id)
    {
       $show = new Show(AdminUser::find($id));

        $show->field('id', 'ID');
        $show->field('username', trans('admin.username'));
        $show->field('name', trans('admin.name'));
        $show->field('roles', trans('admin.roles'))->as(function ($roles) {
            return $roles->pluck('name');
        })->label();
        $show->field('permissions', trans('admin.permissions'))->as(function ($permission) {
            return $permission->pluck('name');
        })->label();
//        $show->field('saleAccounts', trans('销售账号'))->as(function ($saleAccounts) {
//            return collect(getSaleParent($saleAccounts));
//        })->label();
//        $show->field('markets', trans('Market'))->as(function ($markets) {
//            return $markets->pluck('market');
//        })->label();
        $show->field('ips', 'IP')->as(function ($ips) {
            return collect(explode(',', $ips));
        })->label();
        $show->field('created_at', trans('admin.created_at'));
        $show->field('updated_at', trans('admin.updated_at'));

       return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        $permissionModel = config('admin.database.permissions_model');

       $form = new Form(new AdminUser());
       $form->text('name', trans('admin.name'));
//       $form->textarea('content', '文章内容');
        $form->image('avatar', trans('admin.avatar'));

        $form->display('id', 'ID');
        $form->display('username', __('admin.username'));

        $form->multipleSelect('roles', trans('admin.roles'))->options(AdminUser::all()->pluck('name', 'id'));
        $form->multipleSelect('permissions', trans('admin.permissions'))->options($permissionModel::all()->pluck('name', 'id'));
//        $form->multipleSelect('saleAccounts', trans('销售账号'))->options(getSaleParent());


        $form->display('created_at', '创建时间');
        $form->display('updated_at', '更新时间');



        return $form;
    }
}
